home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7550 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.7 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: const char??
  5. Date: 25 Feb 1996 12:09:20 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gqflgINN1kq@keats.ugrad.cs.ubc.ca>
  8. References: <31287436.1235873@news.inforamp.net> <4gb7u8$p5o@sun001.spd.dsccc.com> <TANMOY.96Feb20095538@qcd.lanl.gov>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <TANMOY.96Feb20095538@qcd.lanl.gov>,
  12. Tanmoy Bhattacharya <tanmoy@qcd.lanl.gov> wrote:
  13.  >const is an attribute of the type of an lvalue: When an lvalue with
  14.  >attribute const is used, it means that the code will not change the
  15.  >object using _that_ lvalue.
  16.  >
  17.  >The difference is crucial to understanding function prototypes: when
  18.  >we say that a function is declared `void f(const char *x)', we mean
  19.  >that the function f will not change the object through the lvalue
  20.  >`*x'. The same function may access a global `char y', and it might
  21.  >change y if it feels like, and if you had called f(&y), you can see
  22.  >that *x also got changed by this action! 
  23.  
  24. Precisely. Note that this can also be understood from a purely syntactic
  25. vantage point:
  26.  
  27. In the declaration "const char *x", the "const" is a modifier of the type
  28. "char". The "*x" is a separate syntactic construct which builds on that type.
  29.  
  30. In parameters, you can't have lists of further declarators, but if the
  31. declaration was inside the function body, you could do:
  32.  
  33.     const char *x, y;
  34.  
  35. First, the base type is established as a "const char", a character object
  36. that is read-only. x is a pointer to such an object, but x is itself
  37. modifiable. This means that you can change the value of x to point to any
  38. character, but you cannot modify the character by dereferencing through x.
  39. Thus, x = "abc"; would be legal, but *x = 'x' would not be legal regardless of
  40. where x points. On the other hand, the variable y is just a const char. A read
  41. only character that can be changed only with an intializer. It is valid to do:
  42.  
  43.     const char y = 'a';
  44.  
  45. But not valid to have a subsequent assignment to y.
  46.  
  47.  
  48. The proper syntax for making the pointer itself not modifiable is this: you
  49. declare the basic type, and then complete with a declartor of a constant
  50. pointer. Thus an unmodifiable pointer x to a modifiable integer would be:
  51.  
  52.     int * const x;
  53.  
  54. The trick is to see the declaration of the type, and the further declarators of
  55. objects derived from the type as separate syntactic units. Immediate
  56. understanding is thereafter forthcoming. :)
  57.  
  58.  >(There is a related issue that the function may also change *x by
  59.  >using the lvalue *(char*)x. That, and similar tricks, are so bad
  60.  >programming practices, that I won't even comment on them!)
  61.  
  62. Hmm. Is that not a bit of an error, since the const modifier is being dropped
  63. in the cast? C allows you to only safely convert pointers of any type to void *
  64. and back to the same type. I will have to check references whether or not that
  65. "type" is so restrictive as to include any modifiers like const and volatile.
  66.  
  67. In any case, the cast does make it explicit that you are trying to voluntarily
  68. break the const. But in your function declaration you promised that you would
  69. not touch the object---why go through the bother of making that promise to the
  70. caller, when you intend to violate it?
  71.  
  72.  >An object _defined_ a (i.e. an object whose storage was allocated by a
  73.  >declaration specifying) const, cannot be portably changed by the
  74.  >program. A compiler can assume it's value won't change unless it is
  75.  >also volatile: in which case, even though the program cannot write to
  76.  >it, the compiler has to assume that its value can change due to unkwon
  77.  >reasons. 
  78.  
  79. A read-only device status register would be an example of such a "volatile
  80. const" beast, no?
  81. -- 
  82.  
  83.